﻿using DataAccess;
using DataAccess.Exceptions;
using Model;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Business.WPF
{
    public class NewSubjectManager
    {
        public Subject newSubject(String nameSubject, String acronym, char status, int credits, String description)
        {
            try
            {
                if (credits <= 0)
                {
                    throw new ValidationException("Los créditos no puede ser igual o menor a 0");
                }
                if (String.IsNullOrEmpty(acronym))
                {
                    throw new ValidationException("El acrónimo no puede estar vacio");
                }
                if (String.IsNullOrEmpty(nameSubject))
                {
                    throw new ValidationException("El nombre de la materia no puede estar vacio");
                }                

                if (SubjectDataAccess.getStatusByNameSubjectAndAcronym(nameSubject, acronym) == '0')
                {
                    Subject newSubject = SubjectDataAccess.newSubject(nameSubject, acronym, status, credits, description);
                    if (newSubject == null)
                    {
                        throw new BusinessException("La materia no se creo correctamente, intente de nuevo más tarde");
                    }
                    else
                    {
                        return (newSubject);
                    }
                }
                else
                {
                    throw new BusinessException("Acronimo de la materia existente activo");
                }                
            }
            catch (Exception exception)
            {
                throw new BusinessException("Error: " + exception.Message);
            }            
        }

        public Subject modifySubject(long code, String nameSubject, String acronym, char status, int credits, String description)
        {
            try
            {
                if (credits <= 0)
                {
                    throw new ValidationException("Los créditos no puede ser igual o menor a 0");
                }
                if (String.IsNullOrEmpty(acronym))
                {
                    throw new ValidationException("El acrónimo no puede estar vacio");
                }
                if (String.IsNullOrEmpty(nameSubject))
                {
                    throw new ValidationException("El nombre de la materia no puede estar vacio");
                }

                
                Subject newSubject = SubjectDataAccess.modifySubject(code, nameSubject, acronym, status, credits, description);
                if (newSubject == null)
                {
                    throw new BusinessException("La materia no se modifico correctamente, intente de nuevo más tarde");
                }
                else
                {
                    return (newSubject);
                }
            }
            catch (Exception exception)
            {
                throw new BusinessException("Error: " + exception.Message);
            }
        }
    }
}
